package variable

import "fmt"

func execute() {
	// 标准声明
	var number int
	var str string
	var isOk bool

	fmt.Println(number)
	fmt.Println(str)
	fmt.Println(isOk)

	fmt.Println("====================")

	// 批量声明
	var (
		number1 int
		str1    string
		isOk1   bool
	)

	fmt.Println(number1)
	fmt.Println(str1)
	fmt.Println(isOk1)

	var str2 string = "string"
	var number2 int = 1

	// 或者这样，一次性初始化多个变量
	var str3, number3 = "string", 2

	fmt.Println(str2, number2, str3, number3)

	var str4 = "string"
	var number4 = 4

	// 	4
	// string
	fmt.Println(number4)
	fmt.Println(str4)

	x := "局部变量"
	fmt.Print(x)

	number5, _ := test()
	_, str5 := test()

	fmt.Println(number5)
	fmt.Println(str5)

	const PI = 3.14
	const OTHER = "other"

	const (
		PIL    = 3.14
		OTHERL = "other"
	)

	println(PI, OTHER, PIL, OTHERL)

	// 同时声明多个常量，如果省略了值则和上一行的值相同
	const (
		A = "A"
		B
		C
		D = "D"
		E
	)
	println(A, B, C, D, E) // A A A D D

	const (
		F = iota
		G
		H
		I
		J
	)
	println(F, G, H, I, J) //0 1 2 3 4

	// printStr()
	// changeStr()
	// printType()
	// caseType()
}

func test() (int, string) {
	return 20, "string"
}

func printStr() {
	str := "abcdefg 哈哈哈"
	for i := 0; i < len(str); i++ { // byte 1字节
		fmt.Printf("%v(%c)", str[i], str[i])
	}
	fmt.Println()
	fmt.Println("==========")
	for _, r := range str { // rune 4字节 单个字符
		fmt.Printf("%v(%c)", r, r)
	}
}

func changeStr() {
	str1 := "hello 你好"
	byte1 := []byte(str1)
	fmt.Println(string(byte1)) // 强转
	byte1[0] = 'H'
	byte1[1] = 'H'
	fmt.Println(string(byte1)) // 强转

	str2 := "你好"
	rune2 := []rune(str2)
	fmt.Println(string(rune2)) // 强转
	rune2[0] = '我'
	fmt.Println(string(rune2)) // 强转
}

func printType() {
	var a interface{} = "value" // interface{} 代表变量可以是任何类型，不这么些，执行时会报错
	val, isInt := a.(int)
	fmt.Println(val, isInt)

	val2, isString := a.(string)
	fmt.Println(val2, isString)
}

func caseType() {
	var v interface{} = "string"

	switch v.(type) {
	case int:
		fmt.Print("int")
	case string:
		fmt.Print("string")
	}
}

func changeVarType() {

	// var v = "dd"
	// fmt.Println(int(v))
	// fmt.Println(byte(v))
	// fmt.Println(rune(v))
	// fmt.Println(float32(v))

	var v = 'D'
	fmt.Println(string(v))
	fmt.Println(byte(v))
	fmt.Println(float32(v))
	// fmt.Println(bool(v))
	// fmt.Println(complex64(v))

}
